package com.example.demo.service;

import com.example.demo.entity.BasicUrl;
import com.example.demo.entity.HttpUri;
import com.example.demo.model.ReqBasicUrlMod;
import com.example.demo.model.ReturnMod;
import com.example.demo.repository.BasicUrlRepository;
import com.example.demo.repository.UriRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * Created by chenbo on 2018/5/30.
 */
@Service
public class UriService {

    @Autowired
    private UriRepository uriRepository;

    @Autowired
    private BasicUrlRepository basicUrlRepository;

    @Transactional
    public ReturnMod addUri(){
        ReturnMod mod = new ReturnMod ();
        mod.setCode ( 0 );
        mod.setMsg ( "Service!" );
        mod.setData ( uriRepository.findAll () );
        return mod;
    }

    /**
     * 根据 id 删除接口
     * @param id
     * @return
     */
    @Transactional
    public ReturnMod delUriById( int id){
        ReturnMod mod = new ReturnMod ();
        uriRepository.deleteById ( id );
        mod.setCode ( 0 );
        mod.setMsg ( "成功！" );
        return mod;
    }

    /**
     * 获取所以接口
     * @return
     */
    @Transactional
    public ReturnMod getUris(){
        ReturnMod mod = new ReturnMod ();
        List <HttpUri> uris = uriRepository.findAll ();

        if (uris.isEmpty ()) {
            mod.setCode ( -1 );
            mod.setMsg ( "源数据为空！" );
        }else {
            mod.setCode ( 0 );
            mod.setMsg ( "成功！" );
            mod.setData ( uris );
        }

        return mod;
    }
    /**
     * 添加接口
     * @param uriName  接口名称
     * @param path     接口路径
     * @return
     */
    @Transactional
    public ReturnMod addUri( String uriName, String path){
        ReturnMod mod = new ReturnMod ();

        if (StringUtils.isEmpty ( path )){
            mod.setCode ( -2 );
            mod.setMsg ( "path 路径不能为空" );
            return mod;
        }


        if (StringUtils.isEmpty ( uriName )){
            mod.setCode ( -3 );
            mod.setMsg ( "uriName 不能为空" );
            return mod;
        }

        if (!StringUtils.startsWithIgnoreCase ( path,"/" )) {
            mod.setCode ( -3 );
            mod.setMsg ( "无效的路径:" + path );
            return mod;
        }

        List<HttpUri> uris = uriRepository.findByPathIs ( path );

        if ( uris.isEmpty () ) {
            HttpUri httpUri = new HttpUri ();

            httpUri.setUriName ( uriName );
            httpUri.setPath ( path );
            httpUri.setCreateUser ( 1 );
            httpUri.setUpdateTime ( new Date () );

            HttpUri uri = uriRepository.save ( httpUri );

            mod.setCode ( 0 );
            mod.setMsg ( "添加成功！" );
            mod.setData ( uri );

            return mod;
        }else {
            mod.setCode ( -1 );
            mod.setMsg ( "链接已存在：" + path );
            mod.setData ( uris );
            return mod;
        }
    }

    @Transactional
    public ReturnMod addBasic( ReqBasicUrlMod basicUrlMod ){
        ReturnMod mod = new ReturnMod ();

        HttpUri uri = uriRepository.findById ( basicUrlMod.getId () );

        if ( uri == null ) {
            mod.setCode ( -1 );
            mod.setMsg ( "接口不存在！" );
            return mod;
        }

        if (StringUtils.isEmpty ( basicUrlMod.getBasicUrl ().getHost () )) {
            mod.setCode ( -2 );
            mod.setMsg ( "host 不能为空！" );
            return mod;
        }

        BasicUrl basicUrl = basicUrlRepository.findByHost ( basicUrlMod.getBasicUrl ().getHost () );

        if ( basicUrl != null) {
            mod.setCode ( -3 );
            mod.setMsg ( "host 已存在！" );
            return mod;
        }else {
            basicUrl = new BasicUrl ();
            basicUrl.setHost ( basicUrlMod.getBasicUrl ().getHost () );
            basicUrl.setBasicName ( basicUrlMod.getBasicUrl ().getBasicName () );
            basicUrl.setPort ( basicUrlMod.getBasicUrl ().getPort () );
            basicUrl.setProtocol ( basicUrlMod.getBasicUrl ().getProtocol () );
            basicUrl.setHttpUri ( uri );

            mod.setCode ( 0 );
            mod.setMsg ( "成功！" );
            mod.setData ( basicUrlRepository.save ( basicUrl ) );
            return mod;
        }
    }
}
